# coding: utf-8

# NOTE by zhouhl 2018-07-03
# 抢单策略调整
# 1. 不再使用新订单提醒天数
# 2. 不再使用铜牌会费套餐
# 3. 转化为汇币：
#   3.1 退还汇币 = 新订单提醒剩余天数 * 2
#   3.2 退还汇币 = 铜牌会费剩余天数 * 4

import os
import datetime
import math
import set_env_path

from sqlalchemy import create_engine
from sqlalchemy.sql import text

engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
conn = engine.connect()


def sms_end_date_to_bid_point():
    """
    退还汇币 = 新订单提醒剩余天数 * 2
    BidCostType 为 16
    """

    select_sms_packs_sql = text("""
        select
            id, uid, bid_point, sms_end_date
        from
            pack
        where
            sms_end_date is not null;
    """)

    update_bid_point_sql = text("""
        update
            pack
        set
            bid_point = bid_point + :exchange_bid_point
        where
            id = :pack_id;
    """)

    insert_bid_cost_record_sql = text("""
        insert into
            bid_cost_record
        (
            user_id, cost_type, bid_point, left_bid_point,
            created_at, updated_at
        ) values (
            :uid, 16, :exchange_bid_point, :left_bid_point,
            UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
        );
    """)

    sms_packs = conn.execute(select_sms_packs_sql).fetchall()

    today = datetime.date.today()
    for i in sms_packs:
        uid = i.uid
        pack_id = i.id
        bid_point = i.bid_point
        sms_end_date = i.sms_end_date
        if sms_end_date > today:
            exchange_bid_point = (sms_end_date - today).days * 2

            conn.execute(
                update_bid_point_sql,
                exchange_bid_point = exchange_bid_point,
                pack_id = pack_id,
            )

            conn.execute(
                insert_bid_cost_record_sql,
                uid = uid,
                exchange_bid_point = exchange_bid_point,
                left_bid_point = bid_point + exchange_bid_point,
            )

            print(uid,pack_id,sms_end_date,exchange_bid_point)


def bronze_medal_end_date_to_bid_point():
    """
    退还汇币 = 铜牌会费剩余天数 * 4
    BidCostType 为 17
    """

    select_bronze_packs_sql = text("""
        select
            id, uid, bid_point, bronze_medal_end_date
        from
            pack
        where
            bronze_medal_end_date is not null;
    """)

    update_bid_point_sql = text("""
        update
            pack
        set
            bid_point = bid_point + :exchange_bid_point
        where
            id = :pack_id;
    """)

    insert_bid_cost_record_sql = text("""
        insert into
            bid_cost_record
        (
            user_id, cost_type, bid_point, left_bid_point,
            created_at, updated_at
        ) values (
            :uid, 17, :exchange_bid_point, :left_bid_point,
            UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
        );
    """)

    bronze_packs = conn.execute(select_bronze_packs_sql).fetchall()

    today = datetime.date.today()
    for i in bronze_packs:
        uid = i.uid
        pack_id = i.id
        bid_point = i.bid_point
        bronze_medal_end_date = i.bronze_medal_end_date
        if bronze_medal_end_date > today:
            exchange_bid_point = (bronze_medal_end_date - today).days * 4

            conn.execute(
                update_bid_point_sql,
                exchange_bid_point = exchange_bid_point,
                pack_id = pack_id,
            )

            conn.execute(
                insert_bid_cost_record_sql,
                uid = uid,
                exchange_bid_point = exchange_bid_point,
                left_bid_point = bid_point + exchange_bid_point,
            )

            print(uid,pack_id,bronze_medal_end_date,exchange_bid_point)


if __name__ == '__main__':
    # sms_end_date_to_bid_point()
    # bronze_medal_end_date_to_bid_point()
    pass
